<?php

class ad_table extends model {

    function __construct() {
        parent::__construct(ad_controller::$a_con);
    }

    function ad_admin() {
        return $this->get_Results($this->con->select_query('SELECT * FROM admin', 'ad_admin'));
    }

    function id_temp_check() {
        return $this->get_Results($this->con->select_query('SELECT id,id as name FROM template', 'id_temp_check'), MYSQLI_NUM);
    }

    function select_by_id($table, $name_w, $id) {
        return $this->get_ResultA_One($this->con->select_query('SELECT * FROM ' . $table . ' WHERE ' . $name_w . '=' . $id, 'select_by_id'));
    }

    function insert_last($query) {
        return $this->con->ect_query_id($query, 'sds');
    }

    function ad_buy() {
        return $this->get_Results($this->con->select_query('SELECT * FROM bill', 'ad_buy'));
    }

    function ad_cate() {
        return $this->get_Results($this->con->select_query('SELECT id,cat_name,file_img FROM categories', 'ad_cate'));
    }

    function ad_temp() {
        return $this->get_Results($this->con->select_query('SELECT temp.id,temp_keyword,file_temp,`file_temp_255xX`,cate.cat_name FROM template AS temp INNER JOIN categories AS cate ON temp.id_cat=cate.id', 'ad_cate'));
    }

    function ad_temp_spec() {
        return $this->get_Results($this->con->select_query('SELECT temp.id,temp_keyword,`file_temp_255xX`,cate.cat_name FROM template AS temp INNER JOIN categories AS cate ON temp.id_cat=cate.id WHERE temp.id_cat=0', 'ad_cate'));
    }

    function ad_news() {
        return $this->get_Results($this->con->select_query('SELECT id,news_title,file_news,news_date FROM news', 'ad_acc_type'));
    }

    function ad_menu() {
        return $this->get_Results($this->con->select_query('SELECT id,title FROM web_info', 'ad_acc_type'));
    }

    function ad_customer() {
        return $this->get_Results($this->con->select_query('SELECT * FROM customer', 'ad_bill_cus'));
    }

    function ad_bill() {
        return $this->get_Results($this->con->select_query('SELECT bill_id,bill_date,cus.cus_email,temp.price FROM bill AS bl INNER JOIN customer AS cus ON bl.bill_cus=cus.id INNER JOIN template AS temp ON bl.bill_temp=temp.id', 'ad_bill'));
    }

    function del_ever($table, $id_name, $value) {
        $ex_tbl = explode(',', $table);
        if (count($ex_tbl) <= 1) {
            echo 'ss';
            return $this->con->ect_query('DELETE FROM ' . $table . ' WHERE ' . $id_name . '=' . $value, 'del_ever');
        } else {
            $lst_query = array();
            $i = 0;
            while ($i < count($ex_tbl)) {
                $lst_query[] = 'DELETE FROM ' . $ex_tbl[$i] . ' WHERE ' . $id_name . '=' . $value;
                $i++;
            }
            print_r($lst_query);
            return $this->con->ect_query_trans($lst_query);
        }
    }

    function insert($query) {
        echo $query;
        return $this->con->ect_query($query, 'insert');
    }

    function show_colums($table) {
        return $this->get_Results($this->con->select_query('SHOW COLUMNS FROM ' . $table, 'show_colums'));
    }

    function show_foreign($table) {
        return $this->get_Results($this->con->select_query('SELECT i.TABLE_NAME, k.REFERENCED_TABLE_NAME, k.COLUMN_NAME 
                                                                FROM information_schema.TABLE_CONSTRAINTS i 
                                                                LEFT JOIN information_schema.KEY_COLUMN_USAGE k ON i.CONSTRAINT_NAME = k.CONSTRAINT_NAME 
                                                                WHERE i.CONSTRAINT_TYPE = "FOREIGN KEY" 
                                                                AND i.TABLE_SCHEMA = "' . ad_controller::$a_con[3] . '"
                                                                AND i.TABLE_NAME = "' . $table . '"', 'show_colums'));
    }

    function id_cat_check() {
        return $this->get_Results($this->con->select_query('SELECT id,cat_name FROM categories', 'id_cat_check'), MYSQLI_NUM);
    }

    function bill_temp_check() {
        return $this->get_Results($this->con->select_query('SELECT id,id as name FROM template', 'bill_temp_check'), MYSQLI_NUM);
    }

    function bill_cus_check() {
        return $this->get_Results($this->con->select_query('SELECT id,cus_name FROM template', 'bill_cus_check'), MYSQLI_NUM);
    }

}
